﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using WebService.IServices;
using WebService.Model;

namespace HX_WebService_Core.Controllers
{
    [Route("api/User")]
    [ApiController]
    public class UserController : ControllerBase
    {
        readonly IUserServices userService;
        readonly IRoleServices roleService;
        public UserController(IUserServices user, IRoleServices _roleService)
        {
            userService = user;
            roleService = _roleService;
        }

        [HttpPost("FindPage")]
        public IActionResult FindPage([FromBody] Sys_User users)
        {
            IActionResult response = Unauthorized();
            List<Sys_User> user = null;
            string str = string.Empty;
            if (!string.IsNullOrEmpty(users.Name))
            {
                str = $"select a.*,group_concat(c.remark) RoleNames from sys_user a LEFT JOIN sys_user_role b on a.id = b.user_id LEFT JOIN sys_role c ON b.role_id = c.id WHERE a.`name` LIKE '%{users.Name}%'GROUP BY A.`name`";
                user = userService.QuerySql(str).Result;
            }
            else
            {
                str = $"select a.*,group_concat(c.remark) RoleNames from sys_user a LEFT JOIN sys_user_role b on a.id = b.user_id LEFT JOIN sys_role c ON b.role_id = c.id GROUP BY A.`name`";
                user = userService.QuerySql(str).Result;
            }
            var message = new MessageModel<Sys_User>();
            if (user != null && user.Count > 0)
            {
                message.Msg = null;
                message.Code = 200;
                message.Data = new FindPageMessage() { Content = user, TotalSize = user.Count };
                response = Ok(new { Message = message });
            }
            else
            {
                message.Msg = "未查询到该账户，或者该账户已经删除";
                message.Code = 1;
                message.Data = new FindPageMessage();
                response = Ok(new { Message = message });
            }
            return response;
        }
    }



    class FindPageMessage
    {
        public List<Sys_User> Content { get; set; }
        public int TotalSize { get; set; }
    }
}
